public class LC_0207 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenth1 = 0, lenth2 = 0;
        ListNode nodeA = headA, nodeB = headB;
        while(nodeA != null){
            nodeA = nodeA.next;
            lenth1++;
        }
        while(nodeB != null){
            nodeB = nodeB.next;
            lenth2++;
        }
        nodeA = headA;
        nodeB = headB;
        int key = lenth1 - lenth2;
        if(key > 0){
            while(key != 0){
                key--;
                nodeA = nodeA.next;
            }
        }else if(key < 0){
            while(key != 0){
                key++;
                nodeB = nodeB.next;
            }
        }
        while(nodeA != null){
            if(nodeA == nodeB){
                return nodeA;
            }
            nodeA = nodeA.next;
            nodeB = nodeB.next;
        }
        return null;
    }
}
